// pages/school/index.js
const App = getApp()
const utils = require("../../utils/util.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: "邀请有礼",
        window: {},
        bgImg: "/static/img/yello.jpeg",
        bodyBgColor: "#fdfcfc",
        capsuleBarHeight: App.$getMenuButtonHeight(),
        shareBottom: 300,
        theamTextColor: "#cc9900",//App.globalData.theamTextColor,
        lightningIcon: "/static/img/lightning.png",
        lightningWhiteIcon: "/static/img/lightningWhiteIcon.png",
        isAuth: false,
        myRanking: null,
        oneStart: {
            'nickName': '-',
            'avatar': '/static/img/anon.png',
            'inviteCount': '-',
        },
        twoStart: {
            'nickName': '-',
            'avatar': '/static/img/anon.png',
            'inviteCount': '-',
        },
        threeStart: {
            'nickName': '-',
            'avatar': '/static/img/anon.png',
            'inviteCount': '-',
        },
        start: {
            level1: '/static/img/level1.png',
            level2: '/static/img/level2.png',
            level3: '/static/img/level3.png',
        },
        tabbarHeight: 50,
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        tabbarActive: 0,
        tabs: {
            0: [],
            1: []
        }
        , listData: {
            // 所有用户
            0: {
                list: [],
                effectiveCount: 0,
                giftTip: '',
                page: {
                    pageSize: 20
                }
            }
            // 我邀请的用户
            , 1: {
                list: [],
                giftTip: '',
                effectiveCount: 0,
                page: { pageSize: 20 }
            }
        },
        meInfo: {},
        gift: ''
    },
    URL: {
        tab: {
            0: "/customer/rankingList",
            1: "/customer/getMyCustomer",
            2: "/common/gift.html",
        },
        myRanking: "/customer/myRanking"
    },
    page: {
        home: "/pages/tabbar/index/index",
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 检查是否被邀请进来的
        App.$handlerInviteCode(options);



        this.setData({
            'bgImg': 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(this.data.bgImg, 'base64')
        });


    },

    /**
     *tabbar 高度
     */
    getTabbarHeight(e) {
        let tabbarHeight = e.detail.height
        this.setData({
            tabbarHeight: tabbarHeight
        })
    },
    initData(tabIndex, isPullDown) {
        if (this.data.tabs[tabIndex].length > 0 && !isPullDown) {
            wx.stopPullDownRefresh();
            return;
        }
        let url = this.URL.tab[tabIndex];
        let pageSize = this.data.listData[tabIndex].page.pageSize;
        let tabListKey = 'tabs.' + tabIndex;
        let listKey = 'listData.' + tabIndex + ".list"
        let pageNumKey = 'listData.' + tabIndex + ".page.pageNum"
        let pageSizeKey = 'listData.' + tabIndex + ".page.pageSize"
        let pageTotalKey = 'listData.' + tabIndex + ".page.total"
        let pagesKey = 'listData.' + tabIndex + ".page.pages"
        let effectiveCountKey = 'listData.' + tabIndex + ".effectiveCount"
        let giftKey = 'listData.' + tabIndex + ".giftTip"
        let myRanking = 'listData.' + tabIndex + ".myRanking"

        App.$get(true, url, { pageSize: pageSize }, res => {
            if (isPullDown) {
                wx.stopPullDownRefresh();
            }
            if (res.R) {
                let rows = res.data.page.rows
                this.setData({
                    [pageNumKey]: res.data.page.pageNum,
                    [pageSizeKey]: res.data.page.pageSize,
                    [pageTotalKey]: res.data.page.total,
                    [pagesKey]: res.data.page.pages,
                    [pageSize]: res.data.page.pageSize,
                    [effectiveCountKey]: res.data.effectiveCount,
                    [giftKey]: res.data.giftTip,
                    [myRanking]: res.data.myRanking,
                })
                if (tabIndex == 0) {
                    if (rows.length > 2) {
                        this.setData({
                            'oneStart': rows[0],
                            'twoStart': rows[1],
                            'threeStart': rows[2]
                        })
                    } else if (rows.length > 1) {
                        this.setData({
                            'oneStart': rows[0],
                            'twoStart': rows[1],
                        })
                    } else if (rows.length > 0) {
                        this.setData({
                            'oneStart': rows[0]
                        })
                    }

                }

                this.setData({
                    [tabListKey]: rows,
                    [listKey]: rows,
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.setData({
            isAuth: App.$isAuth()
        })

        // 获取我的排名
        this.getMyranking();

        this.setData({
            'meInfo.avatar': App.$getUserInfo().avatar,
            'meInfo.nickName': App.$getUserInfo().nickName,
            'meInfo.inviteCode': App.$getUserInfo().inviteCode,
        })
        //this.getGiftHtml();
        this.initData(this.data.tabbarActive, false);
    },

    getMyranking() {
        if (App.$isAuth() && this.data.myRanking == null) {
            App.$get(false, this.URL.myRanking, {}, res => {
                if (res.R) {
                    this.setData({
                        myRanking: res.data
                    })
                }
            })
        }
    },

    getGiftHtml() {
        if (utils.isInvalid(this.data.gift)) {
            App.$get(true, this.URL.tab[this.data.tabbarActive], {}, res => {
                if (res.R) {
                    this.setData({
                        gift: res.data
                    })
                }
            })
        }
    },

    onTabbarChange(e) {
        this.setData({
            tabbarActive: e.detail
        })

        if (e.detail === 1) {
            if (!this.data.isAuth) {
                App.$navigateToAuth();
                return;
            }
        }

        this.setShareIcon(e);

        if (e.detail < 2) {
            if (e.detail == 1) {
                this.setLoadingMore(true, "end", "到底了")
            }
            this.initData(e.detail, false)
            return;
        }
        this.getGiftHtml();
    },

    setShareIcon(e) {
        if (e.detail > 0) {
            this.setData({
                shareBottom: 200
            })
        } else {
            let bottom = 300;
            if (!this.data.isAuth) {
                bottom = 200;
            }
            this.setData({
                shareBottom: bottom
            })
        }
    },

    goBack() {
        let pages = getCurrentPages();
        let prePage = utils.getFromPage(App, pages);
        App.$removeStore((pages[pages.length - 1]).route);
        App.$redirectTo(prePage, false);
    },
    goHome() {
        App.$switchTab(this.page.home, false)
    },
    toRulesRemark() {
        App.$redirectTo("/pages/invite/question/index", false)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.data.tabbarActive < 2) {
            this.initData(this.data.tabbarActive, true)
            return;
        }
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let tabIndex = this.data.tabbarActive
        if (tabIndex < 2) {
            if (this.data.listData[tabIndex].page.pageNum + 1 <= this.data.listData[tabIndex].page.pages) {
                this.setLoadingMore(true, "loading", "加载中")
                let url = this.URL.tab[tabIndex];
                let pageSize = this.data.listData[tabIndex].page.pageSize;
                let tabListKey = 'tabs.' + tabIndex;
                let listKey = 'listData.' + tabIndex + ".list"
                let pageNumKey = 'listData.' + tabIndex + ".page.pageNum"
                let pageSizeKey = 'listData.' + tabIndex + ".page.pageSize"
                let pageTotalKey = 'listData.' + tabIndex + ".page.total"
                let pagesKey = 'listData.' + tabIndex + ".page.pages"
                let effectiveCountKey = 'listData.' + tabIndex + ".effectiveCount"
                let giftKey = 'listData.' + tabIndex + ".giftTip"
                App.$get(true, url, {
                    pageNum: this.data.listData[tabIndex].page.pageNum + 1,
                    pageSize: this.data.listData[tabIndex].page.pageSize
                }, res => {
                    if (res.R) {
                        let source = this.data.listData[tabIndex].list
                        this.setData({
                            [pageNumKey]: res.data.page.pageNum,
                            [pageSizeKey]: res.data.page.pageSize,
                            [pageTotalKey]: res.data.page.total,
                            [pagesKey]: res.data.page.pages,
                            [pageSize]: res.data.page.pageSize,
                            [effectiveCountKey]: res.data.effectiveCount,
                            [giftKey]: res.data.giftTip,
                        })

                        res.data.page.rows.forEach(e => {
                            source.push(e)
                        })

                        this.setData({
                            [listKey]: source,
                            [tabListKey]: source
                        })
                    }
                })
                return;
            }
            this.setLoadingMore(true, "end", "到底了")
        }
    },

    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    }

    /**
     * 用户点击右上角分享
     */
    , onShareAppMessage() {
        return App.$onShare('/pages/invite/index', null, "邀请有礼")
    }
})
